package org.bulu.project.web.controller;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bulu.project.common.model.Option;
import org.bulu.project.common.result.Result;
import org.bulu.project.system.enums.LogModuleEnum;
import org.bulu.project.system.model.form.MenuForm;
import org.bulu.project.system.model.query.MenuQuery;
import org.bulu.project.system.model.vo.MenuVO;
import org.bulu.project.system.model.vo.RouteVO;
import org.bulu.project.system.plugin.norepeat.annotation.PreventRepeatSubmit;
import org.bulu.project.system.plugin.syslog.annotation.LogAnnotation;
import org.bulu.project.system.security.SecurityUtils;
import org.bulu.project.system.service.SysMenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜单控制层
 *
 * @author Ray
 * @since 2020/11/06
 */
@RestController
@RequestMapping("/api/v1/menus")
@RequiredArgsConstructor
@Slf4j
public class SysMenuController {

    private final SysMenuService menuService;

    @GetMapping
    @LogAnnotation( value = "菜单列表",module = LogModuleEnum.MENU)
    public Result<List<MenuVO>> listMenus(MenuQuery queryParams) {
        List<MenuVO> menuList = menuService.listMenus(queryParams);
        return Result.success(menuList);
    }

    @GetMapping("/options")
    public Result<?> listMenuOptions(

          @RequestParam(required = false, defaultValue = "false") boolean onlyParent
    ) {
        List<Option> menus = menuService.listMenuOptions(onlyParent);
        return Result.success(menus);
    }

    @GetMapping("/routes")
    public Result<List<RouteVO>> listRoutes() {
        Set<String> roles = SecurityUtils.getRoles();
        List<RouteVO> routeList = menuService.listRoutes(roles);
        return Result.success(routeList);
    }

    @GetMapping("/{id}/form")
    public Result<MenuForm> getMenuForm(
             @PathVariable Long id
    ) {
        MenuForm menu = menuService.getMenuForm(id);
        return Result.success(menu);
    }


    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:menu:add')")
    @PreventRepeatSubmit
    public Result<?> addMenu(@RequestBody MenuForm menuForm) {
        boolean result = menuService.saveMenu(menuForm);
        return Result.judge(result);
    }

    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('sys:menu:edit')")
    public Result<?> updateMenu(
            @RequestBody MenuForm menuForm
    ) {
        boolean result = menuService.saveMenu(menuForm);
        return Result.judge(result);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('sys:menu:delete')")
    public Result<?> deleteMenu(
            @PathVariable("id") Long id
    ) {
        boolean result = menuService.deleteMenu(id);
        return Result.judge(result);
    }

    @PatchMapping("/{menuId}")
    public Result<?> updateMenuVisible(
            @PathVariable Long menuId,
            Integer visible

    ) {
        boolean result = menuService.updateMenuVisible(menuId, visible);
        return Result.judge(result);
    }

}

